package com.zhejiangzhengyuan.municipal_common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhejiangzhengyuan.municipal_common.config.MessageServer;
import com.zhejiangzhengyuan.municipal_common.entity.*;
import com.zhejiangzhengyuan.municipal_common.entity.vo.MessageDetail;
import com.zhejiangzhengyuan.municipal_common.mapper.SmsRecordMapper;
import com.zhejiangzhengyuan.municipal_common.mapper.UserInfoMapper;
import com.zhejiangzhengyuan.municipal_common.mapper.WarnMessageMapper;
import com.zhejiangzhengyuan.municipal_common.service.DeviceService;
import com.zhejiangzhengyuan.municipal_common.service.WarnEventService;
import com.zhejiangzhengyuan.municipal_common.service.WarnMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhejiangzhengyuan.municipal_common.utils.ConstantUtil;
import com.zhejiangzhengyuan.municipal_common.utils.sms.SendMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author lin_ju
 * @create on 2019/12/3
 */
@Service
@Slf4j
public class WarnMessageServiceImpl extends ServiceImpl<WarnMessageMapper, WarnMessage>
    implements WarnMessageService {

  @Autowired private WarnMessageMapper mapper;
  @Autowired private MessageServer messageServer;
  @Autowired private UserInfoMapper userInfoMapper;
  @Autowired private WarnEventService warnEventService;
  @Autowired private DeviceService deviceService;
  @Autowired private SmsRecordMapper smsRecordMapper;

  @Override
  public boolean insertMessage(WarnMessage entity) {
    if (save(entity)) {
      // 刷新指定socket消息列表
      messageServer.refreshSocketMessage(entity.getModuleName(), entity.getReceiveUserId());
      // 发送短信
      UserInfo userInfo = userInfoMapper.selectById(entity.getReceiveUserId());
      WarnEvent warnEvent = warnEventService.getById(entity.getWarnEventId());
      Device device = deviceService.getById(warnEvent.getDeviceId());
      if (Objects.nonNull(userInfo) && Objects.nonNull(userInfo.getPhoneNum())) {
        String message =
            SendMessage.sendErrorMessage(
                userInfo.getPhoneNum(),
                ConstantUtil.getModuleChineseName(entity.getModuleName()),
                device.getName(),
                warnEvent.getReason(),
                "查看详情");
        // 保存记录
        SmsRecord smsRecord =
            new SmsRecord()
                .setSnedTime(entity.getCreateTime())
                .setReceivePhone(userInfo.getPhoneNum())
                .setReceiveUserInfoId(userInfo.getId())
                .setIsSuccess(Strings.isBlank(message))
                .setRemark(message);
        smsRecordMapper.insert(smsRecord);
      }
      return true;
    }
    return false;
  }

  @Override
  public void removeMessageByWarnId(Long warnId) {
    QueryWrapper<WarnMessage> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("warn_event_id", warnId);
    remove(queryWrapper);
    // 刷新消息列表
    messageServer.refreshSocketMessage();
  }

  @Override
  public MessageDetail selectVo(Long messageId) {
    return mapper.selectMessageVo(messageId);
  }
}
